<template>
  <div class="app-container">
    <UploadExcel :on-success="onsuccess" :href="href" />
  </div>
</template>

<script>
import { formatExcelDate } from '@/utils'
import { batchaddemployeesRes } from '@/api/employees'
export default {
  computed: {
    type() {
      return this.$route.query.type
    },
    href() {
      if (this.type === 'employees') {
        return './excel/员工导入模板.xlsx'
      } else {
        return ''
      }
    }
  },
  created() {
    console.log(this.$route.query.type)
  },
  methods: {

    async onsuccess({ results, header }) {
      if (this.type === 'employees') {
        this.importEployess(results)
      }
    },

    async importEployess(results) {
      const userRelations = {
        '入职日期': 'timeOfEntry',
        '手机号': 'mobile',
        '姓名': 'username',
        '转正日期': 'correctionTime',
        '工号': 'workNumber',
        '聘用形式': 'formOfEmployment',
        '组织名称': 'departmentName'
      }
      const list = results.map(item => {
        const obj = {}
        Object.keys(item).forEach(k => {
          if (k === '入职日期' || k === '转正日期') {
            obj[userRelations[k]] = formatExcelDate(item[k])
          }
          obj[userRelations[k]] = item[k]
        })
        return obj
      })
      await batchaddemployeesRes(list)
      this.$message.success('导入成功')
      this.$router.back()
    }

  }
}
</script>

<style lang="scss" scoped>
 .app-container{
     padding: 20px;
 }
</style>
